package cn.sunlelin.educationserver.util;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    /**
     * 邮箱校验
     */
    public static boolean checkEmail(String email) {
        String check = "^([a-zA-Z]|[0-9])(\\w|-)+@[a-zA-Z0-9]+\\.([a-zA-Z]{2,4})$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        return matcher.matches();
    }

    /**
     * 密码校验（长度 6-18，至少包含1个字母）
     */
    public static boolean checkPassword(String password) {
        String check = "(?=.*[a-zA-Z])[a-zA-Z0-9]{6,18}";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(password);
        return matcher.matches();
    }

    /**
     * 随机生成六位数字验证码
     */
    public static String randomSixCode() {
        return String.valueOf(new Random().nextInt(899999) + 100000);
    }

    /*
      随机生成字母
     */
    public static String RandomString(Integer length){
        StringBuilder s = new StringBuilder(50);
        Random random = new Random();
        for( int i = 0; i < length; i ++) {
            int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写还是小写
            s.append((char)(choice + random.nextInt(26)));
        }
        return s.toString();
    }

    /**
     * 随机生成加密盐（4位随机字母 + 4位固定特殊字符）
     */
    public static String randomEncryptedSalt() {
        return RandomString(4) + "#!$@";
    }
}
